﻿using System;
using System.Text;
using ICarPool.ORM.DBUtility;

namespace ICarPool.ORM.Common
{
    public class TableInfo
    {
        public TableInfo()
        {
           // Id = new IdInfo();
            Columns = new ColumnInfo();
            //PropToColumn = new Map();
        }

        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName
        {
            get;
            set;
        }

       /// <summary>
       /// 主键信息
       /// </summary>
        //public IdInfo Id
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// 所有列信息
        /// </summary>
        public ColumnInfo Columns
        {
            get;
            set;
        }

       /// <summary>
       /// 需要转换成列的属性
       /// </summary>
        //public Map PropToColumn
        //{
        //    get;
        //    set;
        //} 

        #region 查询所有字段的SQL
        private string _findAllSQL = string.Empty;
        public string FindAllSQL
        {
            get 
            {
                if (string.IsNullOrEmpty(_findAllSQL))
                {
                    _findAllSQL = string.Format("SELECT {0} FROM {1}", AllProperty, TableName).ToUpper();
                }
                return _findAllSQL;
            }
        }
        #endregion

        #region 根据ID查询所有字段的SQL
        private string _findByIDSQL = string.Empty;
        public string FindByIDSQL
        {
            get
            {
                if (string.IsNullOrEmpty(_findByIDSQL))
                {
                    _findByIDSQL = string.Format(@"SELECT {0} FROM {1} WHERE ID ={2}ID ", AllProperty, TableName, AdoHelper.DbParmChar).ToUpper();
                }

                return _findByIDSQL;
            }
        }
        #endregion

        #region 获取所有字段的SQL
        private string _findAllProSQL = string.Empty;
        public string FindAllProSQL
        {
            get
            {
                if (string.IsNullOrEmpty(_findAllProSQL))
                {
                    _findAllProSQL = string.Format(@"SELECT {0} FROM {1} WHERE ", AllProperty, TableName).ToUpper();
                }

                return _findAllProSQL;
            }
        }
        #endregion

        #region 获取Insert SQL
        private string _insertSQL = string.Empty;
        public string InsertSQL
        {
            get
            {
                if (string.IsNullOrEmpty(_insertSQL))
                {
                    StringBuilder sbColumns = new StringBuilder();
                    StringBuilder sbValues = new StringBuilder();

                    foreach (String key in Columns.Keys)
                    {
                        if (key != "ID")
                        {
                            sbColumns.Append(key).Append(",");
                            sbValues.Append(AdoHelper.DbParmChar).Append(key).Append(",");
                        }
                    }

                    sbColumns.Remove(sbColumns.Length - 1, 1);
                    sbValues.Remove(sbValues.Length - 1, 1);

                    _insertSQL = string.Format("INSERT INTO {0}({1}) VALUES({2})", TableName, sbColumns.ToString(), sbValues.ToString()).ToUpper();
                }
                return _insertSQL;
            }
        }
        #endregion

        #region 获取Update SQL
        private string _updateSQL = string.Empty;
        public string UpdateSQL
        {
            get
            {
                if (string.IsNullOrEmpty(_updateSQL))
                {
                    StringBuilder sbBody = new StringBuilder();

                    foreach (String key in Columns.Keys)
                    {
                        if (key != "ID")
                        {
                            sbBody.Append(key).Append("=").Append(AdoHelper.DbParmChar + key).Append(",");
                        }
                    }

                    sbBody.Remove(sbBody.Length - 1, 1);
                    _updateSQL = string.Format("update {0} set {1} where ID ={2}ID", TableName, sbBody.ToString(), AdoHelper.DbParmChar).ToUpper();
                }
                return _updateSQL;
            }
        }
        #endregion

        #region 获取Delete SQL
        private string _deleteSQL = string.Empty;
        public string DeleteSQL
        {
            get
            {
                if (string.IsNullOrEmpty(_deleteSQL))
                {
                    _deleteSQL = string.Format("delete {0} where ID ={1}ID", TableName, AdoHelper.DbParmChar).ToUpper();
                }
                return _deleteSQL;
            }
        }
        #endregion

        #region 获取所有列
        private string _allProperty = string.Empty;
        public string AllProperty
        {
            get
            {
                if (string.IsNullOrEmpty(_allProperty))
                {
                    _allProperty = GetAllProperty();
                }

                return _allProperty;
            }
        }
        #endregion

        private string GetAllProperty()
        {
            StringBuilder sbColumns = new StringBuilder();

            foreach (String key in this.Columns.Keys)
            {
                sbColumns.Append(key).Append(",");
            }

            sbColumns.Remove(sbColumns.Length - 1, 1);

            return sbColumns.ToString();
        }
    }
}
